package com.bwie.doctor.service.impl;

import com.bwie.common.core.domain.Result;
import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.doctor.domain.po.MedDoctorReviews;
import com.bwie.doctor.domain.po.Symptom;
import com.bwie.doctor.domain.vo.MedDoctorReviewsVo;
import com.bwie.doctor.domain.vo.MedDoctorVo;
import com.bwie.doctor.mapper.MedDoctorReviewsMapper;
import com.bwie.doctor.service.MedDoctorReviewsService;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedDoctorReviewsServiceImpl implements MedDoctorReviewsService {
    @Autowired
    private MedDoctorReviewsMapper medDoctorReviewsMapper;

    /**
     * 患者对医生评价列表
     * @return
     */
    @Override
    public Result queryReviewsList() {
        // 从数据库中获取所有评论
        List<MedDoctorReviewsVo> allReviews = medDoctorReviewsMapper.queryReviewsList();

        Map<Long, MedDoctorReviewsVo> reviewMap = new HashMap<>();
        for (MedDoctorReviewsVo review : allReviews) {
            reviewMap.put(review.getReviewId(), review);
        }

        // 存顶级列表
        ArrayList<MedDoctorReviewsVo> rootReviews = new ArrayList<>();
        // 循环遍历
        for (MedDoctorReviewsVo review : allReviews) {
             //父级
            if (review.getParentReviewid() == null) {
                // 如果是顶级评论，加入根评论列表
                rootReviews.add(review);
            }
                // 查找父评论
                MedDoctorReviewsVo parentReview = reviewMap.get(review.getParentReviewid());
               //子级
                if (parentReview != null) {
                    // 将当前评论添加为其父评论的子评论
                    parentReview.getChildReviews().add(review);
                }
        }

        return Result.success(rootReviews);
    }

    /**
     * 回复评论
     * @param
     */
    @Transactional
    @Override
    public Result queryReviewsDetails(Long reviewId,String reviewText) {
        //根据id查询
        MedDoctorReviews medDoctorReviews = medDoctorReviewsMapper.queryReviewsDetails(reviewId);

        //回复评论
        MedDoctorReviews medDoctor = new MedDoctorReviews();
        medDoctor.setPatientId(medDoctorReviews.getPatientId());
        medDoctor.setMedDoctorId(medDoctorReviews.getMedDoctorId());
        medDoctor.setParentReviewid(reviewId);
        medDoctor.setReviewText(reviewText);
        //添加评论
        medDoctorReviewsMapper.replyReviews(medDoctor);

        return Result.success(medDoctorReviews);
    }

    /**
     * 医生列表
     * @param symptomName
     * @return
     */
    @Override
    public List<MedDoctorVo> queryDoctorList(String symptomName) {
        List<MedDoctorVo> medDoctorVos = medDoctorReviewsMapper.queryDoctorList(symptomName);
        return medDoctorVos;
    }

    /**
     * 症状列表
     * @return
     */
    @Override
    public List<Symptom> querySymptomList(Long partId) {
        return medDoctorReviewsMapper.querySymptomList(partId);
    }


    /**
     * 获取登录人的id
     */
    private Long getLoginUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        return userid;
    }


}
